<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/16
 * Time: 14:55
 */
namespace app\repository;

use think\Exception;

class Repository
{
    //(数据，pid,id,chilren)/返回树形
    public static function treeData($data,$pid = 0,$file_pid='pid',$file_p='id',$key='children'){
        $result = array();
        foreach($data as $v){
            if($v[$file_pid] == $pid){
                $v[$key] = self::treeData($data,$v[$file_p],$file_pid,$file_p,$key);
                $result[] = $v;
            }
        }
        return $result;
    }
    /**
     * 生成token秘钥
     * @param int $digit
     * @return string
     */
    public static function token($digit=1)
    {
        $diction = [
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
            "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9"
        ];
        $dictionLen = count($diction)-1;
        shuffle($diction);
        $tokenStr = '';
        for ($i=0;$i<$digit;$i++){
            $tokenStr .= $diction[mt_rand(0,$dictionLen)];
        }
        return $tokenStr;
    }

    /**生成二维码
     * @param $data
     * @param string $save_path
     * @param string $web_path
     * @param string $level
     * @param string $size
     * @param string $prefix
     * @return string|void
     */
    public function qrcode($data,$save_path='Public/qrcode/',$web_path='/Public/qrcode/',$level='M',$size='5',$prefix='ZETA')
    {
        if($filename = createQRcode($save_path,$data,$level,$size,$prefix)){
            return $web_path.$filename;
        }
        return ;
    }


    /**
     * 发送邮件
     * @param $Receiver-接收邮件地址
     * @param $title-邮件标题
     * @param $body-邮件正文
     * @param $from_title-发件人名称，可选
     * @param string $attachment-附件地址，上传后的地址
     * @return bool|string
     */
    public static function sendMail($Receiver,$title,$body,$from_title=null,$attachment=[],$getcc="",$getbcc="")
    {
        $input = db('systemail')->where('isDefa',1)->find();
        if(count($input)<=0) return 'error';
        $defa = $input;
        $ssl = false;
        if($defa['port']==465) $ssl = true;
        $mail = new Mail();
        $mail->setServer($defa['smtpSever'], $defa['userName'], $defa['userPass'],$defa['port'],$ssl);
        $mail->setFrom($defa['userName']);

        $Receiver = explode(';',$Receiver);
        $mail->setReceiver($Receiver);

        if(!empty($getcc)){
            $getccArr = explode(';',$getcc);
            $mail->setCc($getccArr);
        }
        if(!empty($getbcc)){
            $getbccArr = explode(';',$getbcc);
            $mail->setBcc($getbccArr);
        }

        if(!empty($attachment)){
            $mail->addAttachment($attachment);
        }

        $mail->setMail($title, $body,$from_title);
        $msg = $mail->sendMail();
        if($msg){
            /*try{
                System::log('success','发送邮件-'.$Receiver);
            }catch (Exception $e){}*/
            return 'success';
        }
        /*try{
            System::log('error','发送邮件-'.$Receiver);
        }catch (Exception $e){}*/
        return 'error';
    }
}